﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Commonlib;
namespace AlbonWeb.Manager.AccountManager
{
    public partial class RoleManager : System.Web.UI.Page
    {
        BLL.RoleBLL rolebll = new BLL.RoleBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindRole();
                if (Request["roleid"] != null)
                {
                    initDate(Request["roleid"].ToString());
                }
            }
        }
        //绑定数据
        protected void bindRole()
        {
            string cmdstr = "select * from RoleInfo";
            SqlHelper.BindRepeater(repMain, SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null);
        }

        //初始化要修改的数据 
        private void initDate(string roleid)
        {
            DataTable dt = rolebll.getRoleByID(roleid);
            {
                roleName.Text = dt.Rows[0]["roleName"].ToString();
                startPage.Text = dt.Rows[0]["startPage"].ToString();
                menuList.Text = dt.Rows[0]["menuList"].ToString();
                active.Checked = Boolean.Parse(dt.Rows[0]["active"].ToString());
                remark.Text = dt.Rows[0]["remark"].ToString();
            }
        }

        //删除记录
        protected void imgDelete_Click(object sender, ImageClickEventArgs e)
        {
            int orderid = int.Parse(((ImageButton)sender).CommandArgument);
            if (rolebll.delRole(orderid) == 0)
            {
                lbmessage.Text = "Delete Success";
                bindRole();
            }
            else {
                lbmessage.Text = "There are accounts in this role,please delete them first!";
            }
        }
        //添加或者修改数据
        protected void RoleManager_btnAdd_Click(object sender, EventArgs e)
        {
            Albon.Model.RoleInfo role = new Albon.Model.RoleInfo();
            role.ID = 0;
            if (Request["roleid"] != null)
            {
                role.ID = int.Parse(Request["roleid"].ToString());
            }
            role.roleName = roleName.Text.Trim();
            role.startPage = startPage.Text.Trim();
            role.menuList = menuList.Text.Trim();
            role.active = active.Checked;
            role.remark = remark.Text.Trim();
            rolebll.alterRole(role);
            Response.Redirect("RoleManager.aspx");
        }
    }
}